package mlz.e_lock;

/*
    消费者线程
 */
public class Consumer extends Thread{
    private Resource resource;

    public Consumer(Resource resource) {
        this.resource = resource;
    }


    @Override
    public void run() {
        while (true) {
            //获取锁资源
            Resource.lock.lock();
            //flag是false,消费完成,等待生产
            while(!Resource.flag){
                try {
                    Resource.cust.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("消费者:" + Resource.count + "个");
            //修改标记为消费完成状态
            Resource.flag = false;
            //唤醒生产者线程进行生产任务
            Resource.prod.signal();
            //释放锁
            Resource.lock.unlock();
        }


    }
}










